<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>绘制多个点</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      canvas {
        margin: 50px auto 0;
        display: block;
        background: yellow;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script>
      const canvas = document.getElementById('canvas')
      const gl = canvas.getContext('webgl')

      if (!gl) {
        alert('WebGL not supported in this browser')
        throw new Error('WebGL not supported')
      }
      // 修正顶点着色器源码
      const vsSource = `
        attribute vec4 aPosition;
        attribute float aPointSize;
        void main() {
          gl_Position = aPosition;
          gl_PointSize = aPointSize;
        }
      `
      // 片元着色器
      const fsSource = `
      precision mediump float;
      uniform vec2 uColor;
        void main() {
          gl_FragColor = vec4(uColor.r, uColor.g, 0.0, 1.0);
        }
      `

      // initShader函数实现
      function initShader(gl, vsSource, fsSource) {
        function compileShader(type, source) {
          const shader = gl.createShader(type)
          gl.shaderSource(shader, source)
          gl.compileShader(shader)
          if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            console.error('An error occurred compiling the shaders:', gl.getShaderInfoLog(shader))
            gl.deleteShader(shader)
            return null
          }
          return shader
        }

        const vertexShader = compileShader(gl.VERTEX_SHADER, vsSource)
        const fragmentShader = compileShader(gl.FRAGMENT_SHADER, fsSource)
        const shaderProgram = gl.createProgram()
        gl.attachShader(shaderProgram, vertexShader)
        gl.attachShader(shaderProgram, fragmentShader)
        gl.linkProgram(shaderProgram)

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
          console.error('Unable to initialize the shader program:', gl.getProgramInfoLog(shaderProgram))
          return null
        }

        return shaderProgram
      }

      const program = initShader(gl, vsSource, fsSource)

      gl.useProgram(program)

      const aPosition = gl.getAttribLocation(program, 'aPosition')
      const points = new Float32Array([-0.5, -0.5, 0.5, -0.5, 0.0, 0.5])
      const buffer = gl.createBuffer()
      gl.bindBuffer(gl.ARRAY_BUFFER, buffer)
      gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW)
      gl.vertexAttribPointer(aPosition, 2, gl.FLOAT, false, 0, 0)
      gl.enableVertexAttribArray(aPosition)
      const aPointSize = gl.getAttribLocation(program, 'aPointSize')
      const size = new Float32Array([10.0, 20.0, 30.0])
      const sizebuffer = gl.createBuffer()
      gl.bindBuffer(gl.ARRAY_BUFFER, sizebuffer)
      gl.bufferData(gl.ARRAY_BUFFER, size, gl.STATIC_DRAW)
      gl.vertexAttribPointer(aPointSize, 1, gl.FLOAT, false, 0, 0)
      gl.enableVertexAttribArray(aPointSize)
      // gl.vertexAttrib2f(aPosition, points[i].clickX, points[i].clickY)
      gl.drawArrays(gl.POINTS, 0, 3)
    </script>
  </body>
</html>
